package org.abc.fund.repository.factorRepository;

import org.abc.fund.entity.factorEntity.DataSourceIngestionHistory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 数据源采集历史仓库接口
 */
@Repository
public interface DataSourceIngestionHistoryRepository extends JpaRepository<DataSourceIngestionHistory, Long> {
    
    /**
     * 根据数据源ID查询采集历史
     */
    List<DataSourceIngestionHistory> findByDataSourceIdOrderByStartTimeDesc(Long dataSourceId);
    
    /**
     * 根据数据源ID按创建时间排序查询采集历史
     */
    List<DataSourceIngestionHistory> findByDataSourceIdOrderByCreateTimeDesc(Long dataSourceId);
    
    /**
     * 根据数据源ID分页查询采集历史
     */
    Page<DataSourceIngestionHistory> findByDataSourceIdOrderByStartTimeDesc(Long dataSourceId, Pageable pageable);
    
    /**
     * 根据数据源ID和创建时间分页查询采集历史
     */
    Page<DataSourceIngestionHistory> findByDataSourceIdOrderByCreateTimeDesc(Long dataSourceId, Pageable pageable);
    
    /**
     * 根据数据源ID和状态查询采集历史
     */
    List<DataSourceIngestionHistory> findByDataSourceIdAndStatusOrderByStartTimeDesc(Long dataSourceId, DataSourceIngestionHistory.IngestionStatus status);
    
    /**
     * 根据状态统计数量
     */
    long countByStatus(DataSourceIngestionHistory.IngestionStatus status);
    
    /**
     * 查询最新采集历史记录
     */
    DataSourceIngestionHistory findTopByOrderByCreateTimeDesc();
    
    /**
     * 根据数据源ID和时间范围查询采集历史
     */
    @Query("SELECT h FROM DataSourceIngestionHistory h WHERE h.dataSourceId = :dataSourceId AND h.startTime BETWEEN :startTime AND :endTime ORDER BY h.startTime DESC")
    List<DataSourceIngestionHistory> findByDataSourceIdAndTimeRange(
            @Param("dataSourceId") Long dataSourceId,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime
    );
    
    /**
     * 根据数据源ID查询最近的采集历史
     */
    @Query("SELECT h FROM DataSourceIngestionHistory h WHERE h.dataSourceId = :dataSourceId ORDER BY h.startTime DESC")
    List<DataSourceIngestionHistory> findRecentByDataSourceId(@Param("dataSourceId") Long dataSourceId, Pageable pageable);
    
    /**
     * 查询所有数据源的采集统计信息
     */
    @Query("SELECT h.dataSourceId, h.dataSourceName, COUNT(h) as totalCount, " +
           "SUM(CASE WHEN h.status = 'SUCCESS' THEN 1 ELSE 0 END) as successCount, " +
           "SUM(CASE WHEN h.status = 'FAILED' THEN 1 ELSE 0 END) as failedCount, " +
           "MAX(h.startTime) as lastIngestionTime " +
           "FROM DataSourceIngestionHistory h " +
           "GROUP BY h.dataSourceId, h.dataSourceName")
    List<Object[]> getIngestionStatistics();
    
    /**
     * 根据数据源ID查询采集统计信息
     */
    @Query("SELECT COUNT(h) as totalCount, " +
           "SUM(CASE WHEN h.status = 'SUCCESS' THEN 1 ELSE 0 END) as successCount, " +
           "SUM(CASE WHEN h.status = 'FAILED' THEN 1 ELSE 0 END) as failedCount, " +
           "AVG(h.duration) as avgDuration, " +
           "MAX(h.startTime) as lastIngestionTime " +
           "FROM DataSourceIngestionHistory h WHERE h.dataSourceId = :dataSourceId")
    Object[] getIngestionStatisticsByDataSourceId(@Param("dataSourceId") Long dataSourceId);
    
    /**
     * 查询正在执行的采集任务
     */
    List<DataSourceIngestionHistory> findByStatus(DataSourceIngestionHistory.IngestionStatus status);
    
    /**
     * 根据数据源ID查询正在执行的采集任务
     */
    List<DataSourceIngestionHistory> findByDataSourceIdAndStatus(Long dataSourceId, DataSourceIngestionHistory.IngestionStatus status);
    
    /**
     * 删除指定数据源的所有历史记录
     */
    void deleteByDataSourceId(Long dataSourceId);
    
    /**
     * 删除指定时间之前的历史记录
     */
    void deleteByStartTimeBefore(LocalDateTime time);
} 